/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.agi.util.sound;

import br.com.agi.util.geradordados.GeradorByte;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.*;
import javax.sound.sampled.Line.Info;

/**
 *
 * @author Renato
 */
public class MicrofoneMixer implements FornecedorAudioFormat, GeradorByte{
    private TargetDataLine inputLine;
    private InputConfig config;
    private byte[] buf;
    BuscadorLine<TargetDataLine> buscador;
    
    public MicrofoneMixer(InputConfig inputConfig) throws LineUnavailableException{
        init(inputConfig);
    }
    
    private void init(InputConfig config) throws LineUnavailableException{
        this.config=config;         
        buscador=new InputMixer(config);
        buf=new byte[(int)config.getSampleRate()];
        inputLine= buscador.getLine();
    }
    public MicrofoneMixer() throws LineUnavailableException{
        init(new InputConfig());        
    }  
    
    
    public synchronized void record() throws LineUnavailableException{
        inputLine.open();
        inputLine.start();
        inputLine.read(buf, 0, (int)config.getSampleRate());
        inputLine.stop();
        inputLine.close();
    }      
    
    
    public byte[] getBuf(){
        return buf;
    }

    @Override
    public AudioFormat getAudioFormat() {
        return buscador.getAudioFormat();
    }

    @Override
    public byte[] createData() {
        try {
            record();
        } catch (LineUnavailableException ex) {
            for(int i=0;i<buf.length;i++){
                buf[i]=0;
            }                           
        }
        return buf;
    }
    
}
